import javax.swing.JOptionPane;

public class Login extends javax.swing.JFrame
{

    Usuario[] usuarios;
    
    public Login()
    {
        initComponents();
        
        /*faço a pesquisa no banco e vou jogando os usuarios em uma list*/
        /*coloco apenas os campos ID, Senha, Nome e E-mail*/
        
        usuarios = new Usuario[10];
        Usuario user = new Usuario(123456, "123456", "Marcos", "asd");
        usuarios[0] = user;
    }

    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TextoNome = new javax.swing.JTextField();
        RotuloNome = new javax.swing.JLabel();
        RotuloSenha = new javax.swing.JLabel();
        BotaoLogar = new javax.swing.JToggleButton();
        TextoSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Login"); // NOI18N
        setResizable(false);

        RotuloNome.setText("Nome");

        RotuloSenha.setText("Senha");

        BotaoLogar.setText("Conectar");
        BotaoLogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoLogarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RotuloNome)
                    .addComponent(RotuloSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(BotaoLogar))
                    .addComponent(TextoNome, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                    .addComponent(TextoSenha))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TextoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RotuloNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RotuloSenha)
                    .addComponent(TextoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BotaoLogar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoLogarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoLogarActionPerformed
        boolean logado = false;
        for (int i = 0; ((i < usuarios.length) && !logado); i++)
        {
            if(usuarios[i] != null)
                if (usuarios[i].logar(TextoNome.getText(), TextoSenha.getText()))
                {
                    JOptionPane.showMessageDialog(null, "Logado Com Sucesso!");
                    TextoNome.setText("");
                    TextoSenha.setText("");
                    Index index = new Index(usuarios[i]);
                    logado = true;
                    index.show();
                    this.setEnabled(false);
                }
        }
        if (!logado)
            JOptionPane.showMessageDialog(null, "Login ou Senha inválido!");
    
    }//GEN-LAST:event_BotaoLogarActionPerformed

    
    
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton BotaoLogar;
    private javax.swing.JLabel RotuloNome;
    private javax.swing.JLabel RotuloSenha;
    private javax.swing.JTextField TextoNome;
    private javax.swing.JPasswordField TextoSenha;
    // End of variables declaration//GEN-END:variables
}
